package test.IO;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.EditorKit;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

import org.w3c.tidy.Tidy;

import test.Issue.Issue;
import test.XML.Xpath;
import test.config.Config;

public class Input {
	public static String getAnchorHREFByID(final InputStream inputStream,
			String anchorId) throws BadLocationException, IOException {
		final EditorKit editorKit = new HTMLEditorKit();
		final HTMLDocument document = (HTMLDocument) editorKit
				.createDefaultDocument();
		document.putProperty("IgnoreCharsetDirective", new Boolean(true));
		editorKit.read(new InputStreamReader(inputStream), document, 0);
		final HTMLDocument.Iterator anchors = document.getIterator(HTML.Tag.A);
		while (anchors.isValid()) {
			final AttributeSet attributeSet = anchors.getAttributes();

			String id = (String) attributeSet.getAttribute(HTML.Attribute.ID);
			if (id != null) {
				if (id.equals(anchorId)) {
					final String href = (String) attributeSet
							.getAttribute(HTML.Attribute.HREF);
					return href;
				}
			}
			anchors.next();
		}
		return null;
	}

	public static FileInputStream readFile(String path) {
		try {
			return new FileInputStream(path);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List<Issue> searchForPriorityIssuesToProcess(
			InputStream inputStream, boolean onlyPriority) throws Exception {

		String tidyHTMLPath = Config.getTmpFilePath()+"tidy_temp.html";

		Output.log("Convert HTML -> XHTML:");
		
		tidy(inputStream, tidyHTMLPath);

		return Xpath.getPriorityIssuesToProcess(new FileInputStream(
				tidyHTMLPath), onlyPriority);
	}

	public static boolean isMatch(String patternString, CharSequence input) {
		if (input == null) {
			return false;
		}
		Pattern pattern = Pattern.compile(patternString);
		Matcher matcher = pattern.matcher(input);
		return matcher.matches();
	}

	public static void tidy(InputStream input, String tidyHTMLPath) {
		OutputStream out;
		try {
			out = new FileOutputStream(tidyHTMLPath);
			Tidy tidy = new Tidy();
			tidy.setXHTML(true);
			tidy.setForceOutput(true);
			tidy.parse(input, out);

			out.flush();
			out.close();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static String paramterFromUrlGetMap(String url, String paramId) {
		try {
			HashMap<String, String> parmsMap;
			parmsMap = new HashMap<String, String>();

			String urlParams = url.split("\\?")[1];
			String params[] = urlParams.split("&");

			for (String param : params) {
				String temp[] = param.split("=");
				parmsMap.put(temp[0], java.net.URLDecoder.decode(temp[1],
						"UTF-8"));
			}
			return parmsMap.get(paramId);
		} catch (Exception e) {
			return null;
		}
	}
}
